/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.utn.tpsimulacion02.UI;

import com.utn.tpsimulacion02.ChartManager;
import com.utn.tpsimulacion02.LibExponencial;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author aaltamir
 */
public class VentanaExponencial extends javax.swing.JFrame {

    private List<Double> values = new ArrayList<Double>();
    /**
     * 
     * Creates new form VentanaExponencial
     */
    public VentanaExponencial() {
        initComponents();
        this.setTitle("Calcular distribucion Exponencial");
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.jbGenerarChart.setEnabled(false);
    }
    private void clean(){
        this.jtCantNumeros.setText("");
        this.jtMedia.setText("");
        this.jlListNumeros.setModel(new DefaultListModel());
        this.jbGenerarChart.setEnabled(false);
        values.clear();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jlListNumeros = new javax.swing.JList();
        jtMedia = new javax.swing.JTextField();
        jtCantNumeros = new javax.swing.JTextField();
        jlMedia = new javax.swing.JLabel();
        jlCantNumeros = new javax.swing.JLabel();
        jbGenerarNumeros = new javax.swing.JButton();
        jbGenerarChart = new javax.swing.JButton();
        jbLimpiar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(jlListNumeros);

        jlMedia.setText("Media");

        jlCantNumeros.setText("Cantidad De Numeros");

        jbGenerarNumeros.setText("Generar Numeros");
        jbGenerarNumeros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGenerarNumerosActionPerformed(evt);
            }
        });

        jbGenerarChart.setText("Generar Grafico");
        jbGenerarChart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGenerarChartActionPerformed(evt);
            }
        });

        jbLimpiar.setText("Limpiar");
        jbLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLimpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlCantNumeros)
                            .addComponent(jlMedia))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtCantNumeros, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                            .addComponent(jtMedia)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jbLimpiar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbGenerarChart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbGenerarNumeros, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlCantNumeros)
                            .addComponent(jtCantNumeros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jlMedia)
                            .addComponent(jtMedia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(55, 55, 55)
                        .addComponent(jbGenerarNumeros)
                        .addGap(18, 18, 18)
                        .addComponent(jbLimpiar)
                        .addGap(18, 18, 18)
                        .addComponent(jbGenerarChart))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(51, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbGenerarNumerosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGenerarNumerosActionPerformed
         int cantidad = 0;
        
        if(this.jtCantNumeros.getText().isEmpty()){
            JOptionPane.showMessageDialog(this,"La Cantidad de numeros a generar puede estar vacia");
            return;
        }
        try{
            cantidad =Integer.parseInt(this.jtCantNumeros.getText()) ;
        }catch (NumberFormatException e){
            JOptionPane.showMessageDialog(this, "El valor ingresado no es un numero");
            return;
        }
        if(this.jtMedia.getText().isEmpty()){
                JOptionPane.showMessageDialog(this,"Complete los campos vacios");
                return;
            }
        int media = 0;
            try{
                media = Integer.parseInt(this.jtMedia.getText());
            } catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(this,"El valor ingresado no es un numero");
                return;
            }
            values = LibExponencial.generarNumero(cantidad, media);
            this.jlListNumeros.setListData(values.toArray());
            this.jbGenerarChart.setEnabled(true);
    }//GEN-LAST:event_jbGenerarNumerosActionPerformed

    private void jbLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLimpiarActionPerformed
        this.clean();
    }//GEN-LAST:event_jbLimpiarActionPerformed

    private void jbGenerarChartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGenerarChartActionPerformed
       String str = JOptionPane.showInputDialog(this, "Ingrese la Cantidad de Intervalos que desea", "Generacion de Grafico", 1);
       int cantIntervalos =0;
       if(str !=null){
           try{
               cantIntervalos = Integer.parseInt(str);
           } catch(NumberFormatException e){
               JOptionPane.showMessageDialog(this,"La cantidad de intervalos debe ser un numero entero");
               return;
           }
           ChartManager.generarChart(cantIntervalos,values);
       }
    }//GEN-LAST:event_jbGenerarChartActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbGenerarChart;
    private javax.swing.JButton jbGenerarNumeros;
    private javax.swing.JButton jbLimpiar;
    private javax.swing.JLabel jlCantNumeros;
    private javax.swing.JList jlListNumeros;
    private javax.swing.JLabel jlMedia;
    private javax.swing.JTextField jtCantNumeros;
    private javax.swing.JTextField jtMedia;
    // End of variables declaration//GEN-END:variables
}
